import redis
import logging
from datetime import datetime

class RedisClient:
    def __init__(self, config):
        self.config = config
        self.pool = None
        self.client = None
        self.logger = logging.getLogger(__name__)

    def connect(self):
        """建立Redis连接"""
        self.pool = redis.ConnectionPool(**self.config)
        self.client = redis.Redis(connection_pool=self.pool)
        return self.client

    def get_keys_data(self, keys):
        """获取指定keys的数据"""
        if not self.client:
            raise Exception("Redis连接未建立，请先调用connect()")

        data = []
        current_time = datetime.now().strftime('%Y%m%d_%H%M')
        for key in keys:
            value = self.client.get(key)
            value_str = value.decode('utf-8') if value else 'None'
            data.append([current_time, key, value_str])
        return data

    def close(self):
        """关闭Redis连接"""
        if self.pool:
            self.pool.disconnect()